package com.njtoyo.taxi.admin.library.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.AddShortUrlRequest;
import com.aliyun.dysmsapi20170525.models.AddShortUrlResponse;
import com.yunpian.sdk.YunpianClient;
import com.yunpian.sdk.model.Result;
import com.yunpian.sdk.model.SmsSingleSend;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

/**
 * @Author Dell
 * @Date 2021/5/31 15:49
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class YunpianSmsClient {
    private final YunpianConfig yunpianConfig;
    private final Client client;

    private static final String CODE_TEXT = "【有滴打车】您的验证码是%s。如非本人操作，请忽略本短信";

    //编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    //客服电话
    private static final String REFUSESEND = "400-928-1618";

    public int sendCode(Map<String, String> msg) {
        YunpianClient client = new YunpianClient(yunpianConfig.getSystemApiKey()).init();
        Map<String, String> param = client.newParam(2);
        param.put(YunpianClient.MOBILE, msg.get("mobile"));
        param.put(YunpianClient.TEXT, String.format(CODE_TEXT, msg.get("code")));
        Result<SmsSingleSend> r = client.sms().single_send(param);
        log.info("短信发送状态" + r);
        int code = r.getCode();
        client.close();
        return code;
    }


    /**
     * 司机审核通过
     */
    public void adoptSend(String mobile, String link) throws UnsupportedEncodingException {
        //【有滴打车】您好，您的司机入驻申请已通过。请点击如下链接查看您的登录方式。#link#
        YunpianClient client = new YunpianClient(yunpianConfig.getSystemApiKey()).init();
        Map<String, String> param = client.newParam(3);
        param.put(YunpianClient.MOBILE, mobile);
        param.put(YunpianClient.TPL_ID, String.valueOf(4979258));
        param.put(YunpianClient.TPL_VALUE, URLEncoder.encode("#link#", ENCODING) + "="
                + URLEncoder.encode(link, ENCODING));
        Result<SmsSingleSend> r = client.sms().tpl_single_send(param);
        log.info("司机审核通过短信发送状态" + r);
        client.close();
    }

    /**
     * 司机审核驳回
     */
    public void noadoptSend(String mobile, String link) throws UnsupportedEncodingException {
        //【有滴打车】您好，您的司机入驻申请被驳回。您可点击下方链接查看驳回原因并在修改后再次提交。 #link#

        YunpianClient client = new YunpianClient(yunpianConfig.getSystemApiKey()).init();
        Map<String, String> param = client.newParam(3);
        param.put(YunpianClient.MOBILE, mobile);
        param.put(YunpianClient.TPL_ID, String.valueOf(4979288));
        //模板值
        param.put(YunpianClient.TPL_VALUE, URLEncoder.encode("#link#", ENCODING) + "="
                + URLEncoder.encode(link, ENCODING));
        Result<SmsSingleSend> r = client.sms().tpl_single_send(param);
        log.info("司机审核驳回短信发送状态" + r);
        client.close();
    }

    /**
     * 司机审核拒绝
     */
    public void refuseSend(String mobile) throws UnsupportedEncodingException {
        YunpianClient client = new YunpianClient(yunpianConfig.getSystemApiKey()).init();
        Map<String, String> param = client.newParam(3);
        param.put(YunpianClient.MOBILE, mobile);
        param.put(YunpianClient.TPL_ID, String.valueOf(4979280));
        //模板值
        param.put(YunpianClient.TPL_VALUE, URLEncoder.encode("#contact#", ENCODING) + "="
                + URLEncoder.encode(REFUSESEND, ENCODING));
        Result<SmsSingleSend> r = client.sms().tpl_single_send(param);
        log.info("司机审核拒绝短信发送状态" + r);
        client.close();
    }

    public String addShortUrl(String sourceUrl) {
        AddShortUrlRequest request = new AddShortUrlRequest();
        request.setSourceUrl(sourceUrl)
                .setShortUrlName("司机审核")
                .setEffectiveDays("7");
        try {
            AddShortUrlResponse response = client.addShortUrl(request);
            return response.getBody().getData().getShortUrl();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }

}
